<template>
  <div class="list">
    <!-- 头部 -->
    <myheader>
      <template #left>
        <van-icon name="arrow-left" @click="$router.go(-1)" />
      </template>
      <template #center>
        <span>精彩评论</span>
      </template>
    </myheader>

    <!-- 单个评论 -->
    <div class="item" v-for="item in commentList" :key="item.id">
      <div class="head">
        <img :src="item.user.head_img" alt />
        <div>
          <p>{{ item.user.nickname }}</p>
          <span>时间</span>
        </div>
        <span @click="replay(item)">回复</span>
      </div>
      <commentItem
        v-if="item.parent"
        :commemt="item.parent"
        @replay="replay"
      ></commentItem>
      <div class="text">{{ item.content }}</div>
    </div>

    <!-- 发表评论 -->
    <mybottom
      :article="articleList"
      @refresh="refresh"
      :commentObj="temp"
      @reset="temp = null"
    ></mybottom>
  </div>
</template>

<script>
import myheader from "@/components/myheader";
import { postComment, getDetail } from "@/api/post";
import axios from "@/utils/request";
import commentItem from "@/components/commentItem";
import mybottom from "@/components/mybottom";
export default {
  props: ["id"],
  data() {
    return {
      commentList: [],
      articleList: {},
      focus: false,
      temp: {},
    };
  },
  components: {
    myheader,
    commentItem,
    mybottom,
  },
  async mounted() {
    //渲染数据
    this.init();
    /*   let res = await postComment(this.id);
    //console.log(res);
    this.commentList = res.data.data.map((v) => {
      v.user.head_img = axios.defaults.baseURL + v.user.head_img;
      return v;
    });

    //发表评论的数据(底部)
    let res2 = await getDetail(this.id);
    this.articleList = res2.data.data;
    //console.log(res2); */
  },
  methods: {
    //回复评论点击事件
    replay(item) {
      //点击回复 向子组件传出数据

      this.temp = item;
    },
    //刷新
    refresh() {
      //再次渲染数据
      this.init();
      //让列表回到顶部
      window.scrollTo(0, 0);
    },
    async init() {
      let res = await postComment(this.id);
      //console.log(res);
      this.commentList = res.data.data.map((v) => {
        v.user.head_img = axios.defaults.baseURL + v.user.head_img;
        return v;
      });

      //发表评论的数据(底部)
      let res2 = await getDetail(this.id);
      this.articleList = res2.data.data;
      //console.log(res2);
    },
  },
};
</script>

<style lang="less" scoped>
.item {
  padding: 10px 10px 60px;
  border-bottom: 1px solid #ccc;
  .head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    > img {
      width: 50/360 * 100vw;
      height: 50/360 * 100vw;
      display: block;
      border-radius: 50%;
    }
    > div {
      flex: 1;
      display: flex;
      flex-direction: column;
      margin-left: 10px;
      > span {
        font-size: 12px;
        color: #999;
        line-height: 25px;
      }
    }
    > span {
      color: #999;
      font-size: 13px;
    }
  }
  .text {
    font-size: 14px;
    color: #333;
    padding: 20px 0 10px 0;
  }
}
</style>